/*
 *@本JS功能：镜像分配
 *@本JS作者：杨锡君
 *@编写时间：2018年06月25日
 */
$(function(){
    var Alert1 = new Alert();
    var confirm1 = new Confirm();
    var loading1 = new Loading();
    var $tableBox = $(".box");
    var $searchBtn = $(".searchBtn");
    loading1.init();
    function tabelModel(opt) {
        var str = "";
        if(opt.allocated == false){
            str = '<div>' +
                '<div>' +
                '<div class="oneTitle" >' +
                '<i style="color: '+ opt.courseColor +';" class="iconfont">'+ opt.courseTitle +'</i>' +
                '<p title="'+ opt.courseName +'">'+ opt.courseName +'</p>' +
                '</div>' +
                '<div class="clear"></div>' +
                '<div class="contents">' +
                // '<div class="line"></div>'+
                '<div class="addBox">' +
                '<h4 class="skip" data-name="'+ opt.courseName +'" data-id="'+ opt.courseId +'">' +
                '<img src="pc/images/ico_allocateAdd.png" class="addBtn">' +
                '</h4>' +
                '</div>' +
                '<h4   data-name="'+ opt.courseName +'" data-id="'+ opt.courseId +'" class="skip addBtnDiv">添加</h4>' +
                '</div>' +
                '<div class="clear"></div>' +
                '</div>' +
                '</div>';
        }else{
            str = '<div>' +
                '<div>' +
                '<div class="oneTitle" >' +
                '<i  style="color: '+ opt.courseColor +';" class="iconfont">'+ opt.courseTitle +'</i>' +
                '<p title="'+ opt.courseName +'">'+ opt.courseName +'</p>' +
                '</div>' +
                '<div class="clear"></div>' +
                '<div class="contents">' +
                '<div class="line"></div>'+
                '<div class="oneContent fl">' +
                '<div class="allocateNum">已分配：'+ opt.allocatedCount +'</div>'+
                '<ul class="oneContents">';
            if(opt.images.length != 0){
                for(var i= 0;i < opt.images.length;i++){
                    if(opt.images[i].broken){
                        str+='<li style="color:#C82011" title="'+  opt.images[i].imageType+":"+ opt.images[i].imageName +'">'+opt.images[i].imageType+":"+ opt.images[i].imageName +'</li>';
                    } else {
                        str+='<li title="'+  opt.images[i].imageType+":"+ opt.images[i].imageName +'">'+opt.images[i].imageType+":"+ opt.images[i].imageName +'</li>';
                    }
                }
            }
            str += '</ul>' +
                '</div>' +
                '<div class="clear"></div>' +
                '<div style="background: '+ opt.courseColor +';"  data-name="'+ opt.courseName +'" data-id="'+ opt.courseId +'" class="oneBottom skip ">修改</div>' +
                '</div>' +
                '<div class="clear"></div>' +
                '</div>' +
                '</div>';
        }
        return str;
    }
    var clearStr = "<span class=\"clear\"></span>";
    var imageConfig = {
        some:'',
        str:"",
        init:function(){
            this.bindEvent();
        },
        bindEvent:function(){
            var self = this;
            var json = {
                "pageNum": 1,
                "pageSize": 8
            };

            if(result.paramData!=undefined){
                if(result.paramData.some!=undefined){
                    json = result.paramData.some;
                    if(json.courseName!=undefined){
                        $("#courseName").val(json.courseName);
                    }
                    if(json.isInitial!=undefined){
                        if(json.isInitial){
                            $("#isInitial").val(1);
                        }else{
                            $("#isInitial").val(2);
                        }
                    }
                }
            }
            self.some = json;
            self.tabelList(json);
            $searchBtn.off("click").on("click", function() {

                var jsonStr = {
                    "pageNum": 1,
                    "pageSize": 8
                };
                if($("#courseName").val()!=''){
                    jsonStr.courseName = $("#courseName").val();
                }
                if($("#isInitial").val() != 0) {
                    var isInitial;
                    if ($("#isInitial").val() == 1) {
                        isInitial = true;
                    }
                    if ($("#isInitial").val() == 2) {
                        isInitial = false;
                    }
                    jsonStr.isInitial = isInitial
                }
                self.tabelList(jsonStr);
                self.some = jsonStr;
            });
        },
        skipEvent:function () {
            var self = this;
            $(".skip").off("click").click(function () {
                var json = JSON.stringify({
                    "pageName":"imageAllocateDetail",
                    "paramData":{
                        "courseName":$(this).attr("data-name"),
                        "courseId":Number($(this).attr("data-id")),
                        "some":self.some
                    }
                });
                window.location.href = "skipPage?jsonStr="+json;
            });
        },
        tabelList:function (jsons) {
            var self = this;
            loading1.show();
            // json = JSON.stringify(jsons);
            $.ajax({
                type: "get",
                url: "manage/image/allocate/page",
                data: jsons,
                async: true,
                success: function(msg) {
                    loading1.hide();
                    if(msg.code == 200) {
                        $tableBox.empty();
                        if(msg.data.list.length != 0) {
                            //列表
                            var tabel = "";
                            $.map(msg.data.list, function(opt) {
                                tabel += tabelModel(opt);
                            });
                            $tableBox.append(tabel+clearStr);
                            self.skipEvent();
                        } else {
                            var trImg = "<div style='width: 100%;'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></div>";
                            $tableBox.append(trImg+clearStr)
                        }
                        var sumpage = msg.data.pages;
                        if(sumpage == 0){
                            sumpage = 1;
                        }
                        var nowpage = 1;
                        var pagenumber = 8;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {
                                nowpage = p;
                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                var jsonStr = jsons;
                                jsonStr.pageNum = nowpage;
                                loading1.show();
                                $.ajax({
                                    type: "get",
                                    url: "manage/image/allocate/page",
                                    data: jsonStr,
                                    async: true,
                                    success: function(msg) {
                                        //列表
                                        loading1.hide();
                                        $tableBox.empty();
                                        var tabel = "";
                                        $.map(msg.data.list, function(opt) {
                                            tabel += tabelModel(opt);
                                        });
                                        $tableBox.append(tabel+clearStr);
                                        self.skipEvent();
                                    }
                                });
                            }
                        });
                    } else {
                        Alert1.content = msg.message;
                        Alert1.init();
                        Alert1.show();
                        return false;
                    }

                }
            });
        }

    };
    imageConfig.init();
});
